<?php
namespace Admin\Controller;

use Think\Controller;
use Think\Model;


class StoresetupController extends BaseController {

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
        if (!$this->local_id) $this->local_id = 1;
    }

    /**
     * 店铺配置
     */
    public function index() {
        $m = M('webpos_local_store a');

        $store = M('webpos_local_store')->where(['local_id' => $this->local_id])->find();
        if ($store['services']) {
            $store['services'] = json_decode($store['services'], true);
        }

        if ($_POST) {
            //var_dump($_POST);return;
            //area_dispatch tel mobile ziti_remark wenxintishi
            $area_dispatch = I('area_dispatch', '', htmlspecialchars());
            $tel = I('tel', '', htmlspecialchars());
            $mobile = I('mobile', '', htmlspecialchars());
            $ziti_remark = I('ziti_remark', '', htmlspecialchars());
            $wenxintishi = I('wenxintishi', '');
            $services = I('services', []);
            $maxPriceDesc = I('maxPriceDesc', '');
            $local_log = I('local_log','');
            $services = array_filter($services);
            sort($services);
            $m = M('webpos_local_store');

            $data = [
                'local_log'=>$local_log,
                'area_dispatch' => $area_dispatch,
                'tel' => $tel,
                'mobile' => $mobile,
                'ziti_remark' => $ziti_remark,
                'wenxintishi' => $wenxintishi,
                'maxPriceDesc' => $maxPriceDesc,
                'services' => empty(!$services) ? json_encode($services) : '',
            ];
            try {
                $share = M('weixin_share_logo');
                // 添加店铺logo 为 微信分享店铺小图标
                $shareresult = $share->where(['local_id'=>$this->local_id])->find();
                if ($shareresult) {
                    $share_data1 = [
                        'id' =>$shareresult['id'],
                        'local_id'=> $this->local_id,
                        'image' =>$local_log
                    ];
                    $share->save($share_data1);
                }else{
                    $share_data2 = [
                        'local_id'=> $this->local_id,
                        'image' =>$local_log[0]
                    ];
                    $share->add($share_data2);
                }
                $local_save = $m->where(['local_id' => $this->local_id])->save($data);
                if ($local_save === false) {
                    throw new \Exception(false);
                }
                M()->execute('commit');
                $this->success('修改成功', U('index'));
            }catch (\Exception $e){
                M()->execute('rollback');
                $this->error('修改失败');
            }
            exit;
        }

        $this->assign(['store' => $store]);
        $this->display();
    }

}